package
{
	import com.greensock.TweenLite;
	import com.gui.menu.GUIMenu;
	import com.utils.actionList.ActionListEvent;
	
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import game.IAssetsFactory;
	import game.gamescene.GameScene;
	
	public class View extends EventDispatcher
	{
		protected var _scaffold:Sprite;
		
		///  UI & ASSETS FILE //////////////////////////////
		protected var _gameAssetsFactory:IAssetsFactory;
		protected var _guiFactory:IAssetsFactory;
		
		// GAME SCENE ////
		public var gameScene:GameScene;
		
		public var stripItems:Array;
		public var hitItems:Array;
		public var enemies:Array;
		
		protected var tmpVars:Object;
		
		public function View( scfd:Sprite )
		{
			super(null);
			
			_scaffold = scfd;
			
			tmpVars = {};
		}
		
		public function inputsInitialSetup():void
		{
			trace("inputsInitialSetup");

			onResize(null);
			
			// end of action item
			dispatchEvent( new ActionListEvent(ActionListEvent.EVT_ACTION_LIST_ITEM_COMPLETE) );
		}
		
		protected function onResize(ev:Event = null ):void
		{
		}
		
		/// SETUP SCREEN //////////////////////////////////////////////////////////////////////////////////////
		public function setupScreen():void
		{
			scaffold.stage.scaleMode = StageScaleMode.NO_SCALE;
			scaffold.stage.align     = StageAlign.TOP_LEFT;
			
			onResize();
			dispatchEvent( new ActionListEvent(ActionListEvent.EVT_ACTION_LIST_ITEM_COMPLETE));
		}
		
		/// INIT MENU //////////////////////////////////////////////////////////////////////////////////////
		public function showInitMenu():void
		{
			//tmpVars.menu = assetsFactory.createInstance("com.gui.menu.GUIMenu") as Sprite;
			tmpVars.menu = guiFactory.getElement("GUIMenu") as GUIMenu;
			Sprite(tmpVars.menu).addEventListener( Event.INIT, onMenuStartClick, false, 0, true);
			scaffold.addChild(tmpVars.menu);
		}
		
		protected function onMenuStartClick(ev:Event):void
		{
			ev.target.removeEventListener( Event.INIT, onMenuStartClick );
			
			Sprite(tmpVars.menu).mouseEnabled = false;
			TweenLite.to(Sprite(tmpVars.menu), 0.3, { alpha:0, onComplete:onMenuFadeOutComplete } );
		}
		
		protected function onMenuFadeOutComplete():void
		{
			Sprite(tmpVars.menu).parent.removeChild(Sprite(tmpVars.menu));
			tmpVars.menu = null;
			dispatchEvent( new ActionListEvent(ActionListEvent.EVT_ACTION_LIST_ITEM_COMPLETE ) );
		}
		
		/// GAME SCENE //////////////////////////////////////////////////////////////////////////////////////
		public function setupGameScene(): void
		{
			gameScene = assetsFactory.getElement("GameScene") as GameScene;
			gameScene.alpha = 0;
			//scaffold.addChild(gameScene);
			scaffold.addChildAt(gameScene, 0);
			TweenLite.to(gameScene, 0.3, {alpha:1.0, onComplete:function ():void {
				dispatchEvent( new ActionListEvent(ActionListEvent.EVT_ACTION_LIST_ITEM_COMPLETE ) );
			}});
		}
		
		public function get itemsContainer():MovieClip 
		{
			if (!gameScene ) return null;
			return gameScene.itemsContainer;
		}
		
		//  RESET VARS ////////////////////////////////////////////////////////////////////////////////////////
		public function resetGameVariables():void
		{
			stripItems = [];
			hitItems   = [];
			enemies    = [];
			 
			Mouse.hide();
			
			// end of action item
			dispatchEvent( new ActionListEvent(ActionListEvent.EVT_ACTION_LIST_ITEM_COMPLETE) );
		}
		
		/// EVENTS //////////////////////////////////////////////////////////////////////////////////////
		public function setEventsAndBinds():void
		{
			scaffold.stage.addEventListener(Event.RESIZE,  onResize, false, 0, true);
			
			// end of action item
			dispatchEvent( new ActionListEvent(ActionListEvent.EVT_ACTION_LIST_ITEM_COMPLETE) );
		}
		
		public function showAlert( msg:String ):void
		{
			trace(msg );
		}
		
		public function get scaffold():Sprite { return _scaffold; }
		
		public function get assetsFactory():IAssetsFactory { return _gameAssetsFactory; }
		public function set assetsFactory(val :IAssetsFactory ):void { _gameAssetsFactory = val; }
		public function get guiFactory():IAssetsFactory { return _guiFactory; }
		public function set guiFactory(val :IAssetsFactory ):void { _guiFactory = val; }
	}
}